﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class TimingCategory
    {
        public int Add(Chain.Model.TimingCategory model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into TimingCategory(");
            builder.Append("CategoryName,CategoryFatherID,CategoryShopID,CategoryUserID,CategoryrRemark)");
            builder.Append(" values (");
            builder.Append("@CategoryName,@CategoryFatherID,@CategoryShopID,@CategoryUserID,@CategoryrRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CategoryName", SqlDbType.NVarChar, 50), new SqlParameter("@CategoryFatherID", SqlDbType.Int, 4), new SqlParameter("@CategoryShopID", SqlDbType.Int, 4), new SqlParameter("@CategoryUserID", SqlDbType.Int, 4), new SqlParameter("@CategoryrRemark", SqlDbType.NVarChar, 200) };
            cmdParms[0].Value = model.CategoryName;
            cmdParms[1].Value = model.CategoryFatherID;
            cmdParms[2].Value = model.CategoryShopID;
            cmdParms[3].Value = model.CategoryUserID;
            cmdParms[4].Value = model.CategoryrRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int CategoryID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from TimingCategory ");
            builder.Append(" where CategoryID=@CategoryID OR CategoryFatherID = @CategoryFatherID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.Int, 4), new SqlParameter("@CategoryFatherID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CategoryID;
            cmdParms[1].Value = CategoryID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string CategoryIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from TimingCategory ");
            builder.Append(" where CategoryID in (" + CategoryIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int CategoryID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from TimingCategory");
            builder.Append(" where CategoryID=@CategoryID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CategoryID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select CategoryID,CategoryName,CategoryFatherID,CategoryShopID,CategoryUserID,CategoryrRemark ");
            builder.Append(" FROM TimingCategory ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" CategoryID,CategoryName,CategoryFatherID,CategoryShopID,CategoryUserID,CategoryrRemark ");
            builder.Append(" FROM TimingCategory ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.CategoryID desc");
            }
            builder.Append(")AS Row, T.*  from TimingCategory T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.TimingCategory GetModel(int CategoryID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 CategoryID,CategoryName,CategoryFatherID,CategoryShopID,CategoryUserID,CategoryrRemark from TimingCategory ");
            builder.Append(" where CategoryID=@CategoryID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CategoryID", SqlDbType.Int, 4) };
            cmdParms[0].Value = CategoryID;
            Chain.Model.TimingCategory category = new Chain.Model.TimingCategory();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["CategoryID"] != null) && (set.Tables[0].Rows[0]["CategoryID"].ToString() != ""))
            {
                category.CategoryID = int.Parse(set.Tables[0].Rows[0]["CategoryID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CategoryName"] != null) && (set.Tables[0].Rows[0]["CategoryName"].ToString() != ""))
            {
                category.CategoryName = set.Tables[0].Rows[0]["CategoryName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["CategoryFatherID"] != null) && (set.Tables[0].Rows[0]["CategoryFatherID"].ToString() != ""))
            {
                category.CategoryFatherID = int.Parse(set.Tables[0].Rows[0]["CategoryFatherID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CategoryShopID"] != null) && (set.Tables[0].Rows[0]["CategoryShopID"].ToString() != ""))
            {
                category.CategoryShopID = int.Parse(set.Tables[0].Rows[0]["CategoryShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CategoryUserID"] != null) && (set.Tables[0].Rows[0]["CategoryUserID"].ToString() != ""))
            {
                category.CategoryUserID = int.Parse(set.Tables[0].Rows[0]["CategoryUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CategoryrRemark"] != null) && (set.Tables[0].Rows[0]["CategoryrRemark"].ToString() != ""))
            {
                category.CategoryrRemark = set.Tables[0].Rows[0]["CategoryrRemark"].ToString();
            }
            return category;
        }

        public DataSet GetNifo(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT TimingCategory.*,SysUser.UserName FROM TimingCategory INNER JOIN SysUser ON TimingCategory.CategoryUserID = SysUser.UserID");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM TimingCategory ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.TimingCategory model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update TimingCategory set ");
            builder.Append("CategoryName=@CategoryName,");
            builder.Append("CategoryFatherID=@CategoryFatherID,");
            builder.Append("CategoryShopID=@CategoryShopID,");
            builder.Append("CategoryUserID=@CategoryUserID,");
            builder.Append("CategoryrRemark=@CategoryrRemark");
            builder.Append(" where CategoryID=@CategoryID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@CategoryName", SqlDbType.NVarChar, 50), new SqlParameter("@CategoryFatherID", SqlDbType.Int, 4), new SqlParameter("@CategoryShopID", SqlDbType.Int, 4), new SqlParameter("@CategoryUserID", SqlDbType.Int, 4), new SqlParameter("@CategoryID", SqlDbType.Int, 4), new SqlParameter("@CategoryrRemark", SqlDbType.NVarChar, 200) };
            cmdParms[0].Value = model.CategoryName;
            cmdParms[1].Value = model.CategoryFatherID;
            cmdParms[2].Value = model.CategoryShopID;
            cmdParms[3].Value = model.CategoryUserID;
            cmdParms[4].Value = model.CategoryID;
            cmdParms[5].Value = model.CategoryrRemark;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

